package controllers.wxcp;

import auth.WxCpAuth;
import models.admin.AdminUser;
import models.constants.DeletedStatus;
import models.enums.IssueStatus;
import models.enums.IssueType;
import models.global.Image;
import models.issue.Issue;
import models.issue.IssueDetail;
import org.apache.commons.collections.map.HashedMap;
import play.mvc.Controller;
import play.mvc.With;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.*;


/**
 * Created by LiuBin on 2017/3/1.
 */
@With(WxCpAuth.class)
public class IssueCpController extends Controller {

    public static void issueListAccept() {
        AdminUser adminUser = WxCpAuth.currentUser();
        if(adminUser == null) {
            String msg = "用户不能为空!";
            flash.error(msg);
            redirect("/wxcp/home");
        }
        System.out.println("CP用户ID"+adminUser.id);
        List<Issue> issueList = Issue.findByDetailOprateAndStatus(adminUser,IssueStatus.HANDLING);
        render(issueList);
    }

    public static void issueListVisit() {
        AdminUser adminUser = WxCpAuth.currentUser();
        if(adminUser == null) {
            String msg = "用户不能为空!";
            flash.error(msg);
            redirect("/wxcp/home");
        }
        System.out.println("CP用户ID"+adminUser.id);
        List<Issue> issueList = Issue.findByDetailOprateAndDoubleStatus(adminUser,IssueStatus.HANDLING,IssueStatus.REPAIRMANHANDLE);
        render(issueList);
    }

    public static void issueListEngineer() {
        AdminUser adminUser = WxCpAuth.currentUser();
        if(adminUser == null) {
            String msg = "用户不能为空!";
            flash.error(msg);
            redirect("/wxcp/home");
        }
        System.out.println("CP用户ID"+adminUser.id);
        List<Issue> issueList = Issue.findByDetailOprateAndStatus(adminUser,IssueStatus.ACCEPT);
        render(issueList);
    }

    public static void issueDetailAccept(String issueId) {
        Issue issue = Issue.findByIdAndUnDeleted(issueId);
        List<Image> images = Image.findByRecordId(issue.getId());
        render(issue, images);
    }

    public static void issueDetailVisit(String issueId) {
        Issue issue = Issue.findByIdAndUnDeleted(issueId);
        List<Image> images = Image.findByRecordId(issue.getId());
        render(issue, images);
    }

    public static void issueDetailEngineer(String issueId) {
        Issue issue = Issue.findByIdAndUnDeleted(issueId);
        List<Image> images = Image.findByRecordId(issue.getId());
        render(issue, images);
    }

    public static void accceptIssue(String issueId) {
        Map<String,Object> resultMap = new HashMap<String,Object>();
        Issue issue = Issue.findByIdAndUnDeleted(issueId);
        if(issue == null) {
            resultMap.put("success",false);
            resultMap.put("msg","未找到该报修记录");
            renderJSON(resultMap);
        }
        AdminUser adminUser = WxCpAuth.currentUser();
        if(adminUser == null) {
            String msg = "用户不能为空!";
            flash.error(msg);
            redirect("/wxcp/home");
        }
        Date now = java.sql.Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant());
        IssueDetail.repairManAccept(issue,now,adminUser);
        issue.issueStatus = IssueStatus.REPAIRMANHANDLE;
        issue.save();
        resultMap.put("success",true);
        resultMap.put("msg","受理成功!");
        renderJSON(resultMap);
    }

    public static void repariManVisitForIssue(String issueId,String remark) {
        Map<String,Object> resultMap = new HashMap<String,Object>();
        System.out.println("issueId=:"+issueId);
        Issue issue = Issue.findByIdAndUnDeleted(issueId);
        if(issue == null) {
            resultMap.put("success",false);
            resultMap.put("msg","未找到该报修记录");
            renderJSON(resultMap);
        }
        AdminUser adminUser = WxCpAuth.currentUser();
        if(adminUser == null) {
            String msg = "用户不能为空!";
            flash.error(msg);
            redirect("/wxcp/home");
        }
        Date now = java.sql.Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant());
        IssueDetail.repairManVisitDoor(issue,now,remark,adminUser);
//        issue.remark = content;
        issue.issueStatus = IssueStatus.REPAIRMANDOOR;
        issue.save();
        resultMap.put("success",true);
        resultMap.put("msg","上门登记成功!");
        renderJSON(resultMap);
    }

    public static void engineerVisitForIssue(String issueId,String salary) {
        Map<String,Object> resultMap = new HashMap<String,Object>();
        System.out.println("issueId=:"+issueId);
        Issue issue = Issue.findByIdAndUnDeleted(issueId);
        if(issue == null) {
            resultMap.put("success",false);
            resultMap.put("msg","未找到该报修记录");
            renderJSON(resultMap);
        }
        AdminUser adminUser = WxCpAuth.currentUser();
        if(adminUser == null) {
            String msg = "用户不能为空!";
            flash.error(msg);
            redirect("/wxcp/home");
        }

        Date now = java.sql.Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant());
        IssueDetail.engineerVisit(issue,now,adminUser);
        issue.issueStatus = IssueStatus.ENGINEERDOOR;
        issue.estimateAmount = new BigDecimal(salary.trim());
        issue.save();
        resultMap.put("success",true);
        resultMap.put("msg","预估成功!");
        renderJSON(resultMap);
    }

}
